package com.xiaoq;

import com.xiaoq.coms.util.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chenjh on 2018/1/10.
 */
@Controller
public class BaseEndpoint2B {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate4JedisCluster redisTemplate4JedisCluster;

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    protected String getUserIp() {
        String value = getRequest().getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(value) && !"unknown".equalsIgnoreCase(value)) {
            return value;
        } else {
            return getRequest().getRemoteAddr();
        }
    }

    protected String getShopId(){
        String mgrId = getMgrId();
        String shopId = redisTemplate4JedisCluster.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, mgrId);
        if(StringUtils.isBlank(shopId)){
            //TODO 从数据库里获取
        }
        return shopId;
    }


    protected String getMgrId(){
        return  (String) getRequest().getAttribute("userId");
    }


}
